package nvr.utils;

import java.io.File;
import java.net.*;
import java.util.Enumeration;

public class NvrUtils {
    /**
     * 获取给定 路径的资源路劲
     *
     * @param path
     * @return
     */
    public static String getResourcePath(String path) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL resource = classLoader.getResource("");
        File file = new File(resource.getPath().toString(), path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getPath();
    }

    /**
     * 判断是否是 Linux 系统
     *
     * @return
     */
    public static boolean isLinux() {
        if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
            return false;
        }
        return true;
    }

    /**
     * 获取 本机IP (window linux 通用)
     *
     * @param preferIpv4
     * @param preferIPv6
     * @throws SocketException
     */
    public static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException {
        Enumeration en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface i = (NetworkInterface) en.nextElement();
            String name = i.getName();
            if (!name.contains("docker") && !name.contains("lo")) {
                for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements(); ) {
                    InetAddress addr = (InetAddress) en2.nextElement();
                    if (!addr.isLoopbackAddress()) {
                        if (addr instanceof Inet4Address) {
                            if (preferIPv6) {
                                continue;
                            }
                            return addr;
                        }
                        if (addr instanceof Inet6Address) {
                            if (preferIpv4) {
                                continue;
                            }
                            return addr;
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 返回本机IP
     *
     * @return win或 Linux ip4
     */
    public static String localIp4() {
        try {
            if (isLinux()) {
                return getFirstNonLoopbackAddress(true, false).getHostAddress();
            } else {
                return getWinRealIP();
            }

        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取WindowsIP
     *
     * @return WindowsIP
     */
    public static String getWinRealIP() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();

                // 去除回环接口，子接口，未运行和接口
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }

                if (!netInterface.getDisplayName().contains("Intel") && !netInterface.getDisplayName().contains("Realtek")) {
                    continue;
                }
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ip = addresses.nextElement();
                    if (ip != null) {
                        // ipv4
                        if (ip instanceof Inet4Address) {
                            return ip.getHostAddress();
                        }
                    }
                }
                break;
            }
        } catch (SocketException e) {
            System.err.println("Error when getting host ip address" + e.getMessage());
        }
        return null;
    }
}
